var count = 0;
var l=document.getElementById("name");
var l1=document.getElementById("scaleUnit");
var farmClassifyInfo = [{
    id: "100100100100",
    categoryName: "蔬菜种植",
    categoryIcon: "王贵银1",
    nextCategory: [{
        id: "100101100100",
        categoryName: "蔬菜作物",
        categoryIcon: "s1",
        nextCategory: []
    }]
},{
    id: "100200100100",
    categoryName: "养殖水产",
    categoryIcon: "王贵银1",
    nextCategory: [{
        id: "100201100100",
        categoryName: "水产养殖",
        categoryIcon: "s1",
        nextCategory: []
    }]
}];

function clickCreate(){
    var sure = true;
    var id=$("#name").val();
    var scoale=$("#scale").val();
    var icons = $("#scaleUnit").val();
    var lev = $("#lev").val();
    $("#namea").text("");
    $("#scalea").text("");
    $("#leva").text("");
    if(id==""){
        sure = false;
        $("#namea").text("编号不能为空！");
    }
    if(scoale==""){
        sure = false;
        $("#scalea").text("分类名称不能为空！");
    }
    if(lev==""){
         sure = false;
         $("#leva").text("分类级别不能为空！");
     }
    data1 = {
              "categoryIcon": icons,
              "categoryLevel": lev,
              "categoryName": scoale,
              "id": id
            }
    if(sure){
        $.ajax({
            type:'post',
            url:"/ebpa/category/",
            data:JSON.stringify(data1),
            contentType:"application/json",
            success: function (data) {
                var result = data;
                window.location = "farmclassify.html";
//                    $("#returnInfoShow").text("修改成功！");
//                    location.href="information.html";
            },
            error: function (XMLHttpRequest, textStatus, errorThrown){
            }
        });
    }
}
function clickUpdate(){
    var sure = true;
    var id=$("#name").val();
    var scoale=$("#scale").val();
    var icons = $("#scaleUnit").val();
    var lev = $("#lev").val();
    $("#namea").text("");
    $("#scalea").text("");
    $("#leva").text("");
    if(id==""){
        sure = false;
        $("#namea").text("编号不能为空！");
    }
    if(scoale==""){
        sure = false;
        $("#scalea").text("分类名称不能为空！");
    }
    if(lev==""){
         sure = false;
         $("#leva").text("分类级别不能为空！");
     }
    data1 = {
              "categoryIcon": icons,
              "categoryLevel": lev,
              "categoryName": scoale,
              "id": id
            }
    if(sure){
        $.ajax({
            type:'put',
            url:"/ebpa/category/",
            data:JSON.stringify(data1),
            contentType:"application/json",
            success: function (data) {
                var result = data;
                window.location = "farmclassify.html";
//                    $("#returnInfoShow").text("修改成功！");
//                    location.href="information.html";
            },
            error: function (XMLHttpRequest, textStatus, errorThrown){
            }
        });
    }
}
function clickDelate(){
    var id=$("#name").val();
    $.ajax({
            type: 'delete',
            url: "/ebpa/category/?id="+id,
            dataType: "json",
            async: false,
            success: function (data) {
                var result = data;
                window.location = "farmclassify.html";
            }
        });
}

$(document).ready(function() {
    "use strict";
    showAreaList();
    numSet();
//    getFarmClassifyInfo();
 });
 //点击输入框
 function clickNum(){
    $("#setNum123").removeAttr("class");
             var val=$("#name").val();
             if(val.length<1){
                 val = 0;
             }
             var val1 = 0;
             var val2 = 0;
             var val3 = 0;
             if(val.length>3){
                 val1 = val.substring(0,3);
                 if(val.length>6){
                     val2 = val.substring(3,6);
                     if(val.length>9){
                         val3 = val.substring(6,9);
                     }else{
                         val3 = val.substring(6,val.length);;
                     }
                 }else{
                     val2 = val.substring(3,val.length);
                 }
             }else{
                 val1 = val;
             }
             $("#s1").text(val1);
             $("#s2").text(val2);
             $("#s3").text(val3);
 }
 //对输入的编号进行处理
 function numSet(){
     l.addEventListener("input", function () {
         $("#setNum123").removeAttr("class");
         var val=$(this).val();
         if(val.length<1){
             val = 0;
         }
         var val1 = 0;
         var val2 = 0;
         var val3 = 0;
         if(val.length>3){
             val1 = val.substring(0,3);
             if(val.length>6){
                 val2 = val.substring(3,6);
                 if(val.length>9){
                     val3 = val.substring(6,9);
                 }else{
                     val3 = val.substring(6,val.length);;
                 }
             }else{
                 val2 = val.substring(3,val.length);
             }
         }else{
             val1 = val;
         }
         $("#s1").text(val1);
         $("#s2").text(val2);
         $("#s3").text(val3);
     });
     l1.addEventListener("input", function () {
         $("#setIcon").removeAttr("class");
         var ic = $("#scaleUnit").val();
         $("#setIcon").attr("class",ic);
      });
 }
 //点击按钮显隐分类
function showAreaList() {
    $("#zareaList").find("i").on("click",function (e) {

        if(count ==0){
            $(this).removeAttr("class");
            $(this).attr("class","fa fa-minus");
            $(this).parent("li").parent("div").children("div").removeAttr("class");
           count = 1;
        }else{
            $(this).removeAttr("class");
            $(this).attr("class","fa fa-plus");
            $(this).parent("li").parent("div").children("div").attr("class","tab-pane");
            count = 0;
        }
    });
    $("#zareaList").find("a").on("click",function (e) {
        var nameCode = $(this).attr("title");
        var nameLev = $(this).attr("data-areaid");
        var categoryIcon = $(this).attr("data-content-id");
        var named = $(this).text();
        $("#name").val(nameCode);
        $("#scale").val(named);
        $("#lev").val(nameLev);
        if(categoryIcon==null){
            $("#scaleUnit").val("");
        }else{
            $("#scaleUnit").val(categoryIcon);
        }
        numSet();
        $("#setIcon").removeAttr("class");
        var ic = $("#scaleUnit").val();
        $("#setIcon").attr("class",ic);
    });
}

//获取农场分类
function getFarmClassifyInfo(){
    $.ajax({
        type: 'get',
        url: "/ebpa/category/categoryByList?id=100000000000&level=1",
        dataType: "json",
        async: false,
        success: function (data) {
            farmClassifyInfo = data;
        }
    });
}



var vm = new Vue({
    el:'#vueList',
    data: {
            message:farmClassifyInfo
        },
        created:function(){
        var _self = this;
        $.ajax({
                type: 'get',
                url: "/ebpa/category/categoryByList?id=100000000000&level=1",
                dataType: "json",
                async: false,
                success: function (data) {
                    _self.message  = data;
                }
            });

        }
});